package com.libraryManage.service.impl;

import com.libraryManage.commons.utils.KVModel;
import com.libraryManage.commons.utils.MVCUtil;
import com.libraryManage.commons.utils.ResponseEntity;
import com.libraryManage.entity.BookHomeDo;
import com.libraryManage.entity.HomePageDo;
import com.libraryManage.entity.ManagerDo;
import com.libraryManage.mapper.HomePageMapper;
import com.libraryManage.mapper.ManagerDoMapper;
import com.libraryManage.service.HomePageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Service //这个是对应xml查询语句的注解
@Slf4j
public class HomePageServiceImpl implements HomePageService {

    @Autowired
    HomePageMapper homePageMapper;

    @Autowired
    ManagerDoMapper managerDoMapper;

    /**
     * 首页信息
     * @return
     */
    @Override
    public ResponseEntity<HomePageDo> getHomepage() {

        HomePageDo homePage = new HomePageDo();
//        查询图书总数
        Integer bookCount = homePageMapper.bookCount();
//        查询用户数量
        Integer userCount = homePageMapper.userCount();
//        将b_student表中数据按专业数量
        List<KVModel<String, Long>> major = homePageMapper.selectMajorStatistics();
//        将b_student表中数据按年龄数量
        List<KVModel<Integer, Long>> age = homePageMapper.selectAgeStatistics();

        homePage.setMajor(major);
        homePage.setAge(age);
        homePage.setUserCount(bookCount);
        homePage.setBookCount(userCount);
        Integer days = 7;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
//        计算当前系统时间的前7天的开始时间
        String startDay = sdf.format(new Date(System.currentTimeMillis() - 1000l * 60 * 60 * 24 * days));
//        查询7天前的订单数，并按创建时间分组
        List<KVModel<String, Long>> borrowCountKVList = homePageMapper.selectBorrowCountStatistics(startDay);
//        查询7天前的交易金额，并按创建时间分组
        List<KVModel<String, Long>> outTimeKVList = homePageMapper.selectOutTimeCountStatistics(startDay);

        SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");
        List<Object[]> borrowCount = new LinkedList<>();
//        日期列表
        Object[] borrowCountNameArray = new Object[days];
//        单日订单数量
        Object[] borrowCountValueArray = new Object[days];
//        将对象依次添加到列表中
        borrowCount.add(borrowCountNameArray);
        borrowCount.add(borrowCountValueArray);
        homePage.setDaysBorrow(borrowCount);
        List<Object[]> outTimeCount = new LinkedList<>();
        Object[] outTimeNameArray = new Object[days];
        Object[] outTimeValueArray = new Object[days];
        outTimeCount.add(outTimeNameArray);
        outTimeCount.add(outTimeValueArray);
        homePage.setDaysOutTime(outTimeCount);
        //这里是在补全 group by 为 0 的情况
        for (int i = 0; i < days; i++) {
            Date date = new Date(System.currentTimeMillis() - 1000l * 60 * 60 * 24 * i);//从当前日期前推
            String key = sdfDay.format(date);
//            以日期为索引来查询当前日期的交易量，
            int i1 = borrowCountKVList.indexOf(new KVModel<>(key, null));
            if (i1 >= 0) {
                borrowCountNameArray[days - i - 1] = key;
                borrowCountValueArray[days - i - 1] = borrowCountKVList.get(i1).getValue();
            } else {
                borrowCountNameArray[days - i - 1] = key;
                borrowCountValueArray[days - i - 1] = 0;
            }
//              类似订单统计，7日分别的当日交易金额
            int i2 = outTimeKVList.indexOf(new KVModel<>(key, null));
            if (i2 >= 0) {
                outTimeNameArray[days - i - 1] = key;
                outTimeValueArray[days - i - 1] = outTimeKVList.get(i2).getValue();
            } else {
                outTimeNameArray[days - i - 1] = key;
                outTimeValueArray[days - i - 1] = 0;
            }
        }
        return MVCUtil.response(homePage);
    }

    /**
     * 随机获取图书
     * @return
     */
    @Override
    public ResponseEntity<List<BookHomeDo>> getBookData() {
        List<BookHomeDo> list = homePageMapper.getBookData();
        return MVCUtil.response(list);
    }

    /**
     * 获取用户信息
     * @param managerDo
     * @return
     */
    @Override
    public ResponseEntity<ManagerDo> getUserData(ManagerDo managerDo) {
        log.info(Integer.parseInt(managerDo.getName())+"");
        ManagerDo manager = homePageMapper.getUserData(Integer.parseInt(managerDo.getName()));
        return MVCUtil.response(manager);
    }
}
